<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" href="/js/layui/css/layui.css" media="all">
    <script src="/js/layui/layui.all.js"></script>
    <script src="/admin/js/token.js"></script>

</head>
<body>

<div class="layui-card-body">
    <table class="layui-hide" id="t" lay-filter="t"></table>
</div>
</body>
<script type="text/html" id="caozuo">
    <a class="layui-btn  layui-btn-xs" lay-event="miyaosq">秘钥授权</a>
</script>
<script type="text/javascript">
        var table = layui.table;
        var s = layui.data("s");
        table.render({
            elem: '#t',
            url: '/admin/terminal-key/selectPassword',
            toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
            defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '请选择秘钥',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }]
            ,
            cols: [
                [
                    {
                        field: 'terminal_key_id',
                        title: '秘钥ID',
                        width: '30%',
                        align: 'center'
                    },
                    {
                        field: 'terminal_key_value',
                        title: '秘钥',
                        width: '35%',
                        align: 'center'
                    },
                    {
                        title: '操作',
                        align: 'center',
                        toolbar: '#caozuo',
                        fixed: 'right'
                    }]
            ],
            page: false,
            where: {
                station_id: s.terminal.station_bh,
                terminal_id: s.terminal.terminal_id,
            }
        });
        //监听工具条
        table.on('tool(t)', function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）
            switch (layEvent) {
                case "miyaosq":
                    var sid = layer.confirm("您确定要给该终端设置密钥吗？", {
                            title: "提示",
                            btn: ["标准型", "钟点型", "取消"]
                        }, function (index) {
                            function dateFtt(fmt, date) {
                                var o = {
                                    "M+": date.getMonth() + 1,
                                    "d+": date.getDate(),
                                    "h+": date.getHours(),
                                    "m+": date.getMinutes(),
                                    "s+": date.getSeconds(),
                                };
                                if (/(y+)/.test(fmt))
                                    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
                                for (var k in o)
                                    if (new RegExp("(" + k + ")").test(fmt))
                                        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
                                return fmt;
                            }

                            var beginTime = dateFtt("yyyy-MM-dd hh:mm:ss", new Date());
                            var endTime = dateFtt("yyyy-MM-dd hh:mm:ss", new Date(new Date().getTime() + 24 * 60 * 60 * 1000))
                            var url = "/admin/terminal-key/usePasswordKey";
                            var nowDate = new Date();
                            var nextDate = new Date(nowDate.getTime() + 24 * 60 * 60 * 1000);
                            var room="标准房";
                            var adminId=layui.data("local_store").token;
                            var senddata = {
                                terminal_key_id: data.terminal_key_id,
                                terminal_id: s.terminal.terminal_id,
                                terminal_key_value: data.terminal_key_value,
                                terminal_key_begin_time: beginTime,
                                terminal_key_end_time: endTime,
                                terminal_room:room,
                                terminal_bh:s.terminal.station_bh,
                                admin_id:adminId
                            };
                            var callback = function (res) {
                                if (res.code == 200) {
                                    layer.close(sid);
                                    layer.msg("设置成功", {icon: 1, time: 2000});
                                    setTimeout(function () {
                                        layer.close(layer.index);
                                        window.parent.location.reload();
                                    }, 2000)
                                    return;
                                }
                                else if(res.code==403){
                                    layer.msg("权限不足", {icon: 2});
                                    return;
                                }else{
                                    layer.msg("设置失败", {icon: 2});
                                }
                            }
                            $.get(url, senddata, callback);
                        }, function (index) {
                            layui.data("s", {key: "terminal", value: data});
                            layer.open({
                                    content: 'zhongdian.html?id=' + s.terminal.terminal_id+'&bh='+s.terminal.station_bh,
                                    type: 2,
                                    area: ['500px', '400px']
                                }
                            );
                        }, function (index) {
                            layer.close(sid);
                        }
                    );
                    break;
            }
        })
</script>
</html>
